using System;
using System.Collections.Generic;
using UnityEngine;

namespace BCW.Friends
{
	public class FriendsUITabButton : SelectableButton
	{
		public FriendsUIController.FriendUITab tabType;

		[SerializeField]
		private UILabel _notificationCount;

		private void OnEnable()
		{
			switch (tabType)
			{
			case FriendsUIController.FriendUITab.Friends:
				FriendsController.OnReciveNewFriends += UpdateNotification;
				FriendsController.OnReciveNewFriends += SortFriends;
				FriendsController.OnUpdatePlayerDB += SortFriends;
				break;
			case FriendsUIController.FriendUITab.Invites:
				FriendsController.OnReciveNewInvites += UpdateNotification;
				FriendsController.OnIdsListsUpdated += UpdateNotification;
				FriendInfoUI.updateInvite += UpdateInvite;
				break;
			case FriendsUIController.FriendUITab.Chat:
				FriendsController.OnReciveNewMessages += UpdateNotification;
				FriendsController.OnUpdateMessageCount += UpdateNotification;
				FriendsController.OnIdsListsUpdated += UpdateEnabled;
				FriendInfoUI.OnFriendCountChanged = (Action)Delegate.Combine(FriendInfoUI.OnFriendCountChanged, new Action(UpdateEnabled));
				UpdateEnabled();
				break;
			case FriendsUIController.FriendUITab.Search:
				break;
			}
		}

		private void OnDisable()
		{
			switch (tabType)
			{
			case FriendsUIController.FriendUITab.Friends:
				FriendsController.OnReciveNewFriends -= UpdateNotification;
				FriendsController.OnReciveNewFriends -= SortFriends;
				FriendsController.OnUpdatePlayerDB -= SortFriends;
				break;
			case FriendsUIController.FriendUITab.Invites:
				FriendInfoUI.updateInvite -= UpdateInvite;
				FriendsController.OnReciveNewInvites -= UpdateNotification;
				FriendsController.OnIdsListsUpdated -= UpdateNotification;
				break;
			case FriendsUIController.FriendUITab.Chat:
				FriendsController.OnReciveNewMessages -= UpdateNotification;
				FriendsController.OnUpdateMessageCount -= UpdateNotification;
				FriendsController.OnIdsListsUpdated -= UpdateEnabled;
				FriendInfoUI.OnFriendCountChanged = (Action)Delegate.Combine(FriendInfoUI.OnFriendCountChanged, new Action(UpdateEnabled));
				break;
			}
			selected = false;
		}

		private void OnDestroy()
		{
			switch (tabType)
			{
			case FriendsUIController.FriendUITab.Friends:
				FriendsController.OnReciveNewFriends -= UpdateNotification;
				FriendsController.OnReciveNewFriends -= SortFriends;
				FriendsController.OnUpdatePlayerDB -= SortFriends;
				break;
			case FriendsUIController.FriendUITab.Invites:
				FriendInfoUI.updateInvite -= UpdateInvite;
				FriendsController.OnReciveNewInvites -= UpdateNotification;
				FriendsController.OnIdsListsUpdated -= UpdateNotification;
				break;
			case FriendsUIController.FriendUITab.Chat:
				FriendsController.OnReciveNewMessages -= UpdateNotification;
				FriendsController.OnUpdateMessageCount -= UpdateNotification;
				FriendsController.OnIdsListsUpdated -= UpdateEnabled;
				FriendInfoUI.OnFriendCountChanged = (Action)Delegate.Combine(FriendInfoUI.OnFriendCountChanged, new Action(UpdateEnabled));
				break;
			}
			selected = false;
		}

		public override void Select(bool select)
		{
			if (select && !selected)
			{
				FriendsUIController.instance.OpenTab(tabType);
				SortFriends();
			}
			base.Select(select);
		}

		private void UpdateEnabled()
		{
			if (FriendsController.instance.FriendsIds.Count == 0)
			{
				if (selected)
				{
					FriendsUIController.instance.allButtons[0].OnClick();
				}
				Disable();
			}
			else if (!selected)
			{
				Enable();
			}
		}

		private void SortFriends()
		{
			FriendsUIController.instance.SortGrid<FriendInfoUI>(FriendsUIController.instance.TabFriends.grid, FriendInfoUI.Compare);
		}

		public void ShowNotification(uint count)
		{
			if (count == 0)
			{
				_notificationCount.gameObject.SetActive(false);
				return;
			}
			_notificationCount.text = count.ToString();
			_notificationCount.gameObject.SetActive(true);
		}

		private void UpdateNotification(Dictionary<string, uint> dictMsg)
		{
			UpdateNotification();
		}

		private void UpdateNotification()
		{
			switch (tabType)
			{
			case FriendsUIController.FriendUITab.Friends:
				ShowNotification(FriendsController.instance.CountNewFriends);
				break;
			case FriendsUIController.FriendUITab.Invites:
				ShowNotification((uint)FriendsController.instance.InvitesIds.Count);
				break;
			case FriendsUIController.FriendUITab.Chat:
				ShowNotification(FriendsController.instance.CountNewMessages);
				break;
			case FriendsUIController.FriendUITab.Search:
				break;
			}
		}

		private void UpdateInvite()
		{
			ShowNotification((uint)FriendsController.instance.InvitesIds.Count);
		}
	}
}
